package com.apes.fico.finance.model.gl;

import com.apes.fico.masterdata.gl.model.CashFlow;
import com.apes.framework.jpa.ApesBean;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.fico.masterdata.gl.model.AccountingOperatingChart;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: apestech
 * @description: 凭证分录
 * @author: Chengxm
 * @create: 2018-12-24 14:50
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = GLVoucherItem.CONST_TABLE_NAME)
public class GLVoucherItem implements ApesBean {
    public final static String CONST_TABLE_NAME = "gl_voucher_item";

    @Id
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "财务凭证分录", padding = 8)
    @Comment("凭证分录")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "voucher_id", referencedColumnName = "id")
    @Comment("凭证内码")
    private GLVoucher voucher;

    @Name
    @NotNull
    @ManyToOne()
    @JoinColumn(name = "acc_operating_chart_id", referencedColumnName = "id")
    @Comment("科目")
    private AccountingOperatingChart accountingOperatingChart;

    @Column(name = "debit", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("借方金额")
    private double debit = 0;

    @Column(name = "credit", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("贷方金额")
    private double credit = 0;

    @Comment("摘要")
    @Column(name = "summary", length = 400)
    private String summary;

    @Comment("序号")
    @Column(name = "sequence")
    private Integer sequence;

    @Comment("辅助核算key")
    @Column(name = "collect_aux")
    private String collectAux;

    @Comment("替票")
    @Column(name = "replace_id",columnDefinition = "number(2) default -1")
    private Integer replaceId;

    //排序序号保证查询明细结果不乱序
    @Comment("排序序号")
    @Column(name = "order_num")
    private Integer orderNum;

    @Name
    @OneToMany(mappedBy = "voucherItem", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    private List<GLVoucherAuxiliary> voucherAuxiliaryList = new ArrayList<>();

    public List<GLVoucherAuxiliary> getVoucherAuxiliaryList() {
        if (voucherAuxiliaryList == null || voucherAuxiliaryList.isEmpty()) return new ArrayList<>();
        for (GLVoucherAuxiliary voucherAuxiliary : voucherAuxiliaryList) {
            voucherAuxiliary.setVoucherItem(this);
        }
        return voucherAuxiliaryList;
    }

    public void setVoucherAuxiliaryList(List<GLVoucherAuxiliary> glVoucherAuxiliaries) {
        if (glVoucherAuxiliaries == null) glVoucherAuxiliaries = new ArrayList<>();
        this.voucherAuxiliaryList.clear();
        this.voucherAuxiliaryList.addAll(glVoucherAuxiliaries);
    }


    @Column(name = "aux", length = 400)
    @Comment("辅助核算")
    private String aux;

    public String getAux() {
        if (this.getVoucherAuxiliaryList() == null || voucherAuxiliaryList.isEmpty()) return null;
        List combination = new ArrayList();
        this.getVoucherAuxiliaryList().forEach(voucherAuxiliarys -> {
            String auxName = voucherAuxiliarys.getAccountingOperatingChartAuxiliary().getAccAuxiliaryProject().getName();
            String code = voucherAuxiliarys.getCode();
            String name = voucherAuxiliarys.getName();
            if (auxName != null && name != null) {
                combination.add(auxName + ":" + "[(" +code+")"+ name + "]");
            }
        });
        return combination.toString().substring(1, combination.toString().length() - 1);
    }

    @ManyToOne()
    @JoinColumn(name = "cash_flow_id", referencedColumnName = "id")
    @Comment("现金流量项目")
    private CashFlow cashFlow;

    @Name
    @Transient
    private String key;

    public String getKey() {
        if (!StringUtils.isEmpty(key)) return key;
        if (this.aux == null) return null;
        List combination = new ArrayList();
        this.getVoucherAuxiliaryList().forEach(voucherAuxiliarys -> {
            String id = voucherAuxiliarys.getId();
            String name = voucherAuxiliarys.getAccountingOperatingChartAuxiliary().getAccAuxiliaryProject().getId();
            combination.add(id + ":" + "[" + name + "]");
        });
        return combination.toString().substring(1, combination.toString().length() - 1);
    }

    @Name
    @Transient
    private String name;

    public String getName() {
        if (!StringUtils.isEmpty(name)) return name;
        if (this.aux == null) return null;
        return this.getAux();
    }


}
